// pages/charts/index.js
const app = getApp();
const http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    HTTP: http.ipRoot,
    bottomHeight: app.globalData.model.bottomHeight,
    tabIndex: 0,
    topList: [],
    list: [],
    weekList: [],
    monthList: [],
    totalList: [],
    rule_title: '排行榜规则',
    rulesContent: '',
    jlContent: '',
    jl_title: '',
    isRulesShow: false,
    isJlShow: false
  },
  getData() {
    http.post('api/works/rank', '', 1).then(res => {
      if (res.code == 1) {
        this.setData({
          weekList: res.data.wk_week_rank,
          monthList: res.data.wk_month_rank,
          totalList: res.data.wk_total_rank,
          list: res.data.wk_week_rank.filter(item => item.rank > 3)
        })
        this.setTopList(res.data.wk_week_rank)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  goRules() {
    this.setData({
      isRulesShow: true
    })
  },
  goJianli() {
    this.setData({
      isJlShow: true
    })
  },
  closeRules() {
    this.setData({
      isRulesShow: false,
      isJlShow: false
    })
  },
  // 规则
  getRules() {
    http.post('api/other/article', {
      en: 'ar_rank_rule'
    }).then(res => {
      this.setData({
        rulesContent: res.data.content,
        rule_title: res.data.title
      })
    })
  },
  getJl() {
    http.post('api/other/article', {
      en: 'ar_rank_rewards'
    }).then(res => {
      this.setData({
        jlContent: res.data.content,
        jl_title: res.data.title
      })
    })
  },
  setTopList(list) {
    this.setData({
      topList: list.filter(item => {
        return item.rank < 4
      })
    })
  },
  changeTab(e) {
    var tabIndex = e.currentTarget.dataset.index;
    var list = tabIndex == 0 ? this.data.weekList : tabIndex == 1 ? this.data.monthList : this.data.totalList
    this.setData({
      tabIndex,
      list: list.filter(item => item.rank > 3)
    })
    this.setTopList(list)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
    this.getRules();
    this.getJl();
  },
  onBack() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})